package com.bocloud.cmp.web.controller.stats;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.bocloud.cmp.web.utils.ReportForm;
import com.bocloud.common.enums.BoCloudMethod;
import com.bocloud.common.enums.BoCloudService;
import com.bocloud.common.model.BsmResult;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.MapTools;
import com.bocloud.registry.http.core.ServiceFactory;
import com.bocloud.registry.http.model.RemoteService;

@RestController
@RequestMapping("/platform")
public class PlatformController {

	private final String BASE_SERVICE = "/platform";
	private static final BoCloudService SERVICE = BoCloudService.Cmp;
	@Autowired
	private ServiceFactory serviceFactory;

	/**
	 * 得到平台的概要信息
	 * 
	 * @param params
	 * @return
	 */
	@RequestMapping(value = "/abstracts", method = { RequestMethod.GET })
	public BsmResult abstracts(@RequestParam(value = Common.PARAMS, required = true) String params,
			HttpServletRequest request) {
		Map<String, Object> paramMap = MapTools.simpleMap(Common.PARAMS, params);
		RemoteService service = serviceFactory.safeBuild(SERVICE, BASE_SERVICE + "/abstracts", BoCloudMethod.BASIC,
				null, paramMap, request);
		return service.invoke();
	}

	/**
	 * 得到平台的宿主机列表
	 * 
	 * @param page
	 * @param rows
	 * @param params
	 * @param sorter
	 * @param type
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/hosts", method = { RequestMethod.POST })
	public BsmResult hosts(@RequestParam(value = Common.PAGE, required = false, defaultValue = Common.ONE) Integer page,
			@RequestParam(value = Common.ROWS, required = false, defaultValue = Common.TEN) Integer rows,
			@RequestParam(value = Common.PARAMS, required = false) String params,
			@RequestParam(value = Common.SORTER, required = false) String sorter,
			@RequestParam(value = Common.TYPE, required = false) String type, HttpServletRequest request) {
		Map<String, Object> param = assembleParam(page, rows, params, sorter, type);
		String url = BASE_SERVICE + "/hosts";
		RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.LIST, null, param, request);
		return service.invoke();
	}

	/**
	 * 得到平台的告警列表
	 * 
	 * @param page
	 * @param rows
	 * @param params
	 * @param sorter
	 * @param type
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/alarms", method = { RequestMethod.POST })
	public BsmResult alarms(
			@RequestParam(value = Common.PAGE, required = false, defaultValue = Common.ONE) Integer page,
			@RequestParam(value = Common.ROWS, required = false, defaultValue = Common.TEN) Integer rows,
			@RequestParam(value = Common.PARAMS, required = false) String params,
			@RequestParam(value = Common.SORTER, required = false) String sorter,
			@RequestParam(value = Common.TYPE, required = false) String type, HttpServletRequest request) {
		Map<String, Object> param = assembleParam(page, rows, params, sorter, type);
		String url = BASE_SERVICE + "/alarms";
		RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.LIST, null, param, request);
		return service.invoke();
	}

	private Map<String, Object> assembleParam(Integer page, Integer rows, String params, String sorter, String type) {
		Map<String, Object> param = new HashMap<>();
		if (null == page || 0 >= page) {
			param.put(Common.PAGE, Common.ONE);
		} else {
			param.put(Common.PAGE, page.toString());
		}
		if (null == rows || 0 >= rows) {
			param.put(Common.ROWS, Common.TEN);
		} else {
			param.put(Common.ROWS, rows.toString());
		}
		if (StringUtils.isNotEmpty(params)) {
			param.put(Common.PARAMS, params);
		}
		if (StringUtils.isNotEmpty(sorter)) {
			param.put(Common.SORTER, sorter);
		}
		if (StringUtils.isNotEmpty(type)) {
			param.put(Common.TYPE, type);
		}
		return param;
	}

	/**
	 * 得到平台的概要信息
	 * 
	 * @param params
	 * @return
	 */
	@RequestMapping(value = "/export", method = { RequestMethod.GET })
	public BsmResult export(@RequestParam(value = Common.PARAMS, required = true) String params,
			HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> paramMap = MapTools.simpleMap(Common.PARAMS, params);
		JSONObject object = JSONObject.parseObject(params);
		Long vendorId = object.getLong("vendorId");

		RemoteService service = serviceFactory.safeBuild(SERVICE, BASE_SERVICE + "/export", BoCloudMethod.BASIC, null,
				paramMap, request);
		BsmResult result = service.invoke();

		String type = JSONObject.parseObject(result.getData().toString()).get("type").toString();
		if (result.isSuccess()) {// 根据结果进行报表导出
			if (null == vendorId && StringUtils.isEmpty(type)) {// 没有vendorID与类型即是要导出全部数据
				return new BsmResult(false, "平台类型出错，无法导出数据");
			} else {
				return ReportForm.createPdf(result, response);
			}
		} else {
			return new BsmResult(false, "获取数据失败，无法导出报表");
		}
	}
}
